// ----------------------------------------------整体-----------------------------------

import Cookie from './cookie.js'
import Api from  './api.js'
import WApi from  '../wj/api.js'

let url="http://127.0.0.1:8080"
let people=null         //就餐人数
new Vue({
    el:'#sign_up',
    data(){
        return{
            uinfos:[],                  //------当前用户的所有信息
            search:'',                  //------搜索框内容
            checked:false,              //已阅读框
            activity:[],               //选中的饭局餐厅名
            fanJoin:[],                 //获取所有已发布的饭局（Activity 表）
            applyInfo:'',               //申请理由
            bzInfo:'',                  //备注（忌口）
            status:'',                  //饭局状态
            payStatus:'',               //支付状态
            applyfanju:[],              //申请饭局的人        （fanJuJoin 表）
            applypeople:'',             //当前是第几个申请人
            isapplyFanju:true,           //是否能够申请
        }
    },
    methods: {
        open(max) {
            this.$prompt('请输入就餐人数', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputValidator:function (value) {
                    value=parseInt(value)
                    max=parseInt(max)
                    if(value>max || value<=0)
                        return false
                    else {
                        people=value
                    }
                },
                inputErrorMessage: '人数超过上限或者小于0'
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消输入'
                });
            });
        },

        //获取个人信息
        async info () {
            let zh = sessionStorage.getItem("zh")
            let allinfo = await WApi.login()
            for(let i=0;i<allinfo.length;i++){
                if(allinfo[i].zh==zh){
                    this.uinfos=allinfo[i]
                    break
                }
            }
            let obj = Cookie.getCookie("bm")
            let max = Cookie.getCookie("maxpeople")

            this.initData(obj)
            this.open(max)
        },
        initData(obj) {                              //---------进行姓名、昵称、头像的赋值------------
            this.fanJoin=this.getActivitys()
            this.fuzhi(this.fanJoin,obj)
        },
        fuzhi(fanju,obj){
          for (let i=0;i<fanju.length;i++){
              if(fanju[i].id==obj){
                  this.activity=fanju[i]
                  if(this.activity.code==0){
                      this.status='饭局未满'
                  }else {
                      this.status='饭局已满'
                  }
              }
          }
        },
        reset(){
            window.location.href='./Sign_up.html'
        },
        getActivitys() {
            let msg = null
            $.post(url+"/ActivityServlet",{
                type:"selectAll",
            }, function (data){
                msg=data.data
            },"json")
            return msg
        },
        commit(){
            this.applyfanju=Api.selectAll()
            this.applypeople=Api.getCountByfanjuId(this.applyfanju,this.activity.id)
            this.isapplyFanju=Api.isapplyFanju(this.applyfanju,this.uinfos.id,this.activity.id)
            alert(this.isapplyFanju)
            alert("最大人数"+this.activity.max_people)
            alert("当前人数"+this.applypeople)
            // alert(this.isapplyFanju)
            // alert(this.applypeople>this.activity.max_people)
            // alert((this.applypeople>this.activity.max_people)|| (!this.isapplyFanju))
            // alert(this.applypeople>this.activity.max_people)
            this.dateTrue()
        },
        dateTrue(){
            if(this.checked==true){

                if(this.applyInfo && this.bzInfo){
                    if((this.applypeople>this.activity.max_people)|| (!this.isapplyFanju) || people==null){
                        this.$alert('已满或者重复申请,或者是没有输入就餐人数', '友情提示', {
                            confirmButtonText: '确定',
                        });
                    }else{
                        let uinfos=this.uinfos
                        let activity=this.activity
                        let applypeople=this.applypeople
                        $.post(url+'/FanJuJoinServlet',{
                            type:'addFanJuJoin',
                            userid:this.uinfos.id,
                            fanjuid:this.activity.id,
                            status:this.status,
                            payStatus:'未支付',
                            applyReason:this.applyInfo,
                            remarks1:this.bzInfo,
                            remarks2:people
                        },function f(data) {
                            if("申请饭局成功"==data.message){
                                // alert(applypeople)
                                function order(){
                                    let uinfos          //当前用户信息
                                    let activity        //当前饭局信息
                                    let applypeople     //当前是第几个申请人
                                    let applynumber     //就餐人数
                                }
                                let order2 = new order()
                                order2.uinfos=uinfos
                                order2.activity=activity
                                order2.applypeople=applypeople
                                order2.applynumber=people
                                alert(JSON.stringify(order2))
                                Cookie.setCookie("order",JSON.stringify(order2),"h1")
                                // Cookie.setCookie("uinfos",JSON.stringify(uinfos),"h1")
                                // Cookie.setCookie("activity",JSON.stringify(activity),"h1")
                                // Cookie.setCookie("applypeople",applypeople,"h1")
                                location.href="Success.html"
                            }
                        },"json")
                    }

                }else {
                    this.$alert('写一点增加成功率', '小提示', {
                        confirmButtonText: '确定',
                    });
                }
            }else {
                this.$alert('请先阅读并同意条款', '信息合法性检测', {
                    confirmButtonText: '确定',
                });
            }

        }
    },
    mounted() {
        //-------------加载页面初始化数据--------------------------
        if(sessionStorage.getItem("zh")==null){
            this.$alert('请先登录', '系统提示', {
                confirmButtonText: '确定',
                callback: action => {
                    this.$message({
                        type: 'warning',
                        message: '即将跳转到登录界面...'
                    });
                    location.href='./login.html'
                }
            });
        }else{
            this.info()
        }
        //--------------------------------------------------------
    }
})
